<template>
  <div class="topic-management">
    <div class="head">
      <!-- 顶部工具栏 -->
      <el-form size="small" :inline="true">
        <el-form-item label="搜索">
          <el-input v-model="searchInput" placeholder="输入题目编号或名称进行检索"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain icon="el-icon-plus" size="small" @click="openAddDialog">新增题目</el-button>
        </el-form-item>
        <el-form-item>
          <el-button :disabled="selectedTopics.length === 0" type="danger" plain icon="el-icon-delete" size="small" @click="deleteTopics">删除题目</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="body">
      <!-- 题目列表 -->
      <el-table :data="filteredTopics" style="width: 100%">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="topicId" label="题目编号" sortable>
        </el-table-column>
        <el-table-column prop="topicName" label="题目名称">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="scope">
            <el-button size="mini" @click="openEditDialog(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑或新增题目的对话框 -->
    <el-dialog title="题目详情" :visible.sync="dialogVisible">
      <el-form :model="currentTopic">
        <el-form-item label="题目编号">
          <el-input v-model="currentTopic.topicId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="题目名称">
          <el-input v-model="currentTopic.topicName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveTopic">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchInput: '',
      topics: [
        { topicId: '001', topicName: '题目一' },
        { topicId: '002', topicName: '题目二' }
      ],
      selectedTopics: [],
      dialogVisible: false,
      currentTopic: { topicId: '', topicName: '' }
    };
  },
  computed: {
    filteredTopics() {
      return this.topics.filter((topic) => topic.topicName.includes(this.searchInput));
    }
  },
  methods: {
    openAddDialog() {
      this.currentTopic = { topicId: '', topicName: '' };
      this.dialogVisible = true;
    },
    openEditDialog(topic) {
      this.currentTopic = { ...topic };
      this.dialogVisible = true;
    },
    saveTopic() {
      if (!this.currentTopic.topicId) { // 如果没有ID，认为是新添加
        this.topics.push({ ...this.currentTopic, topicId: Math.random().toString(16).slice(2, 10) });
      } else { // 否则为编辑现有题目
        const index = this.topics.findIndex(t => t.topicId === this.currentTopic.topicId);
        if (index !== -1) {
          this.topics[index] = { ...this.currentTopic };
        }
      }
      this.dialogVisible = false;
    },
    deleteTopics() {
      this.topics = this.topics.filter(topic => !this.selectedTopics.includes(topic.topicId));
      this.selectedTopics = [];
    }
  }
};
</script>

<style scoped>
.topic-management {
  padding: 20px;
}
</style>
